package com.oauth.google.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.oauth.application.client.rpc.ApplicationRemoteService;
import com.oauth.application.client.rpc.ApplicationRemoteServiceAsync;
import com.oauth.auth.client.beans.MemberClientBean;
import com.oauth.auth.client.rpc.MemberRemoteService;
import com.oauth.auth.client.rpc.MemberRemoteServiceAsync;
import com.oauth.components.client.application.MyApplications;
import com.oauth.components.client.beans.ApplicationClientBean;
import com.oauth.components.client.rpc.ApplicationUnSubscribeRemoteService;
import com.oauth.components.client.rpc.ApplicationUnSubscribeRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.components.client.ui.footer.FooterUI;
import com.oauth.components.client.ui.header.HeaderUI;
import com.oauth.google.client.ui.GoogleTabPanel;

public class GoogleEntryPoint implements EntryPoint {

	private final MemberRemoteServiceAsync memberService = GWT.create(MemberRemoteService.class);

	private final ApplicationRemoteServiceAsync applicationService = GWT
			.create(ApplicationRemoteService.class);

	private static GoogleEntryPointUiBinder uiBinder = GWT.create(GoogleEntryPointUiBinder.class);

	private final ApplicationUnSubscribeRemoteServiceAsync applicationRemoveService = GWT
			.create(ApplicationUnSubscribeRemoteService.class);

	interface GoogleEntryPointUiBinder extends UiBinder<DockLayoutPanel, GoogleEntryPoint> {
	}

	@UiField
	HeaderUI header;

	@UiField
	FooterUI footer;

	@UiField
	FlowPanel centerPanel;

	@UiField
	VerticalPanel westPanel;

	@UiField
	MyApplications myApplications;

	@UiField
	GoogleTabPanel googlePanel;

	public void onModuleLoad() {
		DockLayoutPanel p = uiBinder.createAndBindUi(this);

		// header.getLoadingLabel().setText("loading...");
		memberService.getMember(new OAuthAsnyCallBack<MemberClientBean>() {
			public void onSuccess(MemberClientBean result) {
				header.setUserName(result.getEmail());
			}
		});

		applicationService
				.getMyApplications(new OAuthAsnyCallBack<ArrayList<ApplicationClientBean>>() {
					public void onSuccess(ArrayList<ApplicationClientBean> result) {
						myApplications.addApplications(result, applicationRemoveService);
					}
				});

		googlePanel.initTabPanel();

		RootLayoutPanel rp = RootLayoutPanel.get();
		rp.add(p);
		// RootPanel.get().add(dockPanel);
	}

}